<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用于检测业务代码死循环或者长时间阻塞等问题
 * 如果发现业务卡死，可以将下面declare打开（去掉//注释），并执行php start.php reload
 * 然后观察一段时间workerman.log看是否有process_timeout异常
 */
//declare(ticks=1);

use \GatewayWorker\Lib\Gateway;

/**
 * 主逻辑
 * 主要是处理 onConnect onMessage onClose 三个方法
 * onConnect 和 onClose 如果不需要可以不用实现并删除
 */
class Events
{
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     * 
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        echo "connect";
        // 向当前client_id发送数据 
//        Gateway::sendToClient($client_id, "Hello $client_id\r\n");
//        // 向所有人发送
//        Gateway::sendToAll("$client_id login\r\n");
    }
    
   /**
    * 当客户端发来消息时触发
    * @param int $client_id 连接id
    * @param mixed $message 具体消息
    */
   public static function onMessage($client_id, $message)
   {
       echo "message";
       $message_data=json_decode($message,true);
       $redis = new Redis();
       $redis->connect('172.17.0.1', 6379);
       $message_data['data'] = json_encode($message_data['data']);

       switch ($message_data['type']) {
           //登录
           case "login":
               echo "login";
               //绑定玩家
                if ($message_data['client_type']=="player"){
                    //定义玩家组
                    $group=$message_data['game_id'].'_'.$message_data['client_type'];
                    Gateway::bindUid($client_id,$message_data['game_id'].'_game_'.$message_data['player_id']);
                    Gateway::joinGroup($client_id,$group);
                    //发送给玩家
                    Gateway::sendToGroup($group,$message_data['data']);
                    //发送给ob
                    Gateway::sendToUid($message_data['game_id'].'_game_ob',$message_data['data']);
                    //发送玩家信息给客户端
                    //判断是否存在
                    $play_name=$redis->hExists($group, $message_data['player_id']);
                    // if (!$play_name){//不存在
                        //为hash表中的字段赋值。成功返回1，失败返回0。若hash表不存在会先创建表再赋值，若字段已存在会覆盖旧值。
                        $redis->hSet($group,$message_data['player_id'],$message_data['data']);
                    // }
                        $players = $redis->hGetAll($group);
                        $players['type'] = "players_data";
                        $players = json_encode($players);
                        Gateway::sendToUid($message_data['game_id'].'_game_'.$message_data['player_id'],$players);
                }else{//绑定ob
                    //定义ob组
                    $group=$message_data['game_id'].'_ob';
                    Gateway::bindUid($client_id,$message_data['game_id'].'_game_ob');
                    Gateway::joinGroup($client_id,$group);

                    $players = $redis->hGetAll($message_data['game_id'].'_player');
                    $players['type'] = "players_data";
                    $players = json_encode($players);
                    Gateway::sendToUid($message_data['game_id'].'_game_ob',$players);
                }
               break;
           case "die":
                echo "die";
                //保存信息
                $group=$message_data['game_id'].'_'.$message_data['client_type'];
                $player=$redis->hGet($group, $message_data['player_id']);
                $str_live = json_decode($player, true);
                $str_live['live'] = false;
                $str_live['type'] = "die";
                $str_live = json_encode($str_live);
                $redis->hSet($group,$message_data['player_id'],$str_live);
                //发送给玩家
                Gateway::sendToGroup($message_data['game_id'].'_'.$message_data['client_type'],$str_live);
                //发送给ob
                Gateway::sendToUid($message_data['game_id'].'_game_ob',$str_live);
                break;
           case "letdie":
                echo "letdie\n";
                $group=$message_data['game_id'].'_player';
                $data = json_decode($message_data['data'], true);
                $player=$redis->hGet($group, $data['userInfo']);
                $str_live = json_decode($player, true);
                $str_live['live'] = false;
                $str_live['type'] = 'letdie';
                $str_live = json_encode($str_live);
                $redis->hSet($group,$data['userInfo'],$str_live);
                //发送给玩家
                Gateway::sendToGroup($group,$str_live);
                //发送给ob
                Gateway::sendToUid($message_data['game_id'].'_game_ob',$str_live);
                break;

               break;
           case "position":
               echo "position";
               //推送玩家位置到ob
               $redis->set($message_data['player_id'],$message_data['data']);
               Gateway::sendToUid($message_data['game_id'].'_game_ob',$message_data['data']);
               $group=$message_data['game_id'].'_player';
               Gateway::sendToGroup($group,$message_data['data']);
               break;
           case "get_position":
               echo "get_position";
               //推送位置给玩家
               $position=$redis->get($message_data['player_id']);
               Gateway::sendToUid($message_data['game_id'].'_game_'.$message_data['player_id'],$position);
               break;
           case "message":
               echo "message";
               Gateway::sendToGroup($message_data['game_id'].'_player',$message_data['data']);
               break;
           case "start":
               echo "start";
               if($message_data['client_type'] != 'ob')return;
               $group=$message_data['game_id'].'_player';
               Gateway::sendToGroup($group,$message_data['data']);
               break;
           case "restart":
               echo "restart";
               if($message_data['client_type'] != 'ob')return;
               $group=$message_data['game_id'].'_player';
               $list = $redis->hGetAll($group);
               var_export($list);
               foreach($list as $k=>$v){
                $redis->hDel($group, $k);
               }
               break;
       }
        // 向所有人发送
//        Gateway::sendToAll("$client_id said $message\r\n");
   }
   
   /**
    * 当用户断开连接时触发
    * @param int $client_id 连接id
    */
   public static function onClose($client_id)
   {
       // 向所有人发送 
//       GateWay::sendToAll("$client_id logout\r\n");
       echo "$client_id logout\r\n";
   }
}
